import axios from 'axios';
import { IlineInfo, IstationDetail } from '../type/lineInfoType';

const lineInfo = async function (line: number): Promise<IlineInfo> {
  const URL = 'https://www.longyan.cn/bus_wp/busline.json';
  let lineInfoData: IlineInfo;
  await axios
    .all(
      [0, 1].map((isUpDown) =>
        axios.get(URL, {
          params: {
            getLineDetail: true,
            lineNo: line,
            isUpDown: isUpDown,
          },
        }),
      ),
    )
    .then(
      axios.spread(({ data: lineUp }, { data: lineDown }) => {
        lineInfoData = lineUp;
        if (lineDown.retcode == '001') {
          lineDown.data.stationDetails.map(
            (station: IstationDetail) => (station.labelNo = -station.labelNo),
          );
          lineInfoData.data.stationDetails.push(
            ...lineDown.data.stationDetails,
          );
        }
      }),
    )
    .catch((err) => console.log(err));

  return lineInfoData;
};

export { lineInfo };
